package com.init.iblog.Controller;

import com.init.iblog.domain.ResponseResult;
import com.init.iblog.domain.entity.LoginUser;
import com.init.iblog.domain.entity.Menu;
import com.init.iblog.domain.entity.User;
import com.init.iblog.domain.vo.AdminUserInfoVo;
import com.init.iblog.domain.vo.RoutersVo;
import com.init.iblog.domain.vo.UserInfoVo;
import com.init.iblog.enums.AppHttpCodeEnum;
import com.init.iblog.exception.SystemException;
import com.init.iblog.service.AdminLoginService;

import com.init.iblog.service.MenuService;
import com.init.iblog.service.RoleService;
import com.init.iblog.utils.BeanCopyUtils;

import com.init.iblog.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户登录控制层
 */
@RestController
@CrossOrigin
public class AdminLoginController {

    @Autowired
    private AdminLoginService adminLoginService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleService roleService;


    /**
     * 用户登录
     * @param user
     * @return
     */
    @PostMapping("/user/login")
    public ResponseResult userLogin(@RequestBody User user){
        //前端需要做校验，后端也需要做校验(有些请求可能不是来源于前端页面的请求)
        System.out.println("++++++++++++++++进入/user/login接口查询用户信息++++++++++++++++");
        System.out.println("===============前端传来的参数user"+user.toString());
        if(!StringUtils.hasText(user.getName())){
            //提示  必须需要传入用户名
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return adminLoginService.userLogin(user);
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/user/logout")
    public ResponseResult logout(){

        System.out.println("++++++++++++++++进入/user/logout接口用户退出登录++++++++++++++++");

        return adminLoginService.logout();
    }

    /**
     * 获取登录用户信息
     * @return
     */
    @GetMapping("/getInfo")
    public ResponseResult<AdminUserInfoVo> getInfo(){
        System.out.println("++++++++++++++++进入/getInfo接口获取登录用户信息++++++++++++++++");
        //获取当前登录的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //根据用户id查询权限信息
        List<String> perms = menuService.selectPermsByUserId(loginUser.getUser().getId());
        //根据用户id查询角色信息

        System.out.println("-=============perms="+perms);
        List<String> roles = roleService.selectRoleKeyByUserId(loginUser.getUser().getId());

        //获取用户信息，封装成VO
        User user = loginUser.getUser();
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user, UserInfoVo.class);
        //封装数据返回

        AdminUserInfoVo adminUserInfoVo = new AdminUserInfoVo(perms,roles,userInfoVo);
        return ResponseResult.okResult(adminUserInfoVo);
    }


    /**
     * 根据用户的角色不同，在管理系统中返回不同的管理系统功能菜单界面
     */
    @GetMapping("/getRouters")
    public ResponseResult<RoutersVo> getRouters(){
        System.out.println("++++++++++++++++进入/getRouters接口根据用户的角色不同，在管理系统中返回不同的管理系统功能菜单界面++++++++++++++++");
        //查询menu 结果是tree的形式(子父菜单)
        Long userId = SecurityUtils.getUserId();
        List<Menu> menus = menuService.selectRouterMenuTreeByUserId(userId);
        //封装暑假返回

        return ResponseResult.okResult(new RoutersVo(menus));
    }

}
